<template>
  <j-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="标题" >
          <a-input placeholder="请输入标题" v-decorator="['title', {rules: [ { required: true, message: '请输入标题!' },]}]" />
        </a-form-item>

        <a-form-item 
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="内容">
          <j-editor ref="editor" v-decorator="['content', {}]" :triggerChange=true />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="类型">
          <j-dict-select-tag v-decorator="['type', {rules: [ { required: true, message: '请选择类型!' },]}]" title="类型" :triggerChange=true dictCode="carMembrane" />
        </a-form-item>
       
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'
  import JUpload from '@/components/jeecg/JUpload'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import AddressSelect from '../comp/AddressSelect'
  import JEditor from '@/components/jeecg/JEditor'

  export default {
    name: "CommonModal",
    components: { JUpload,JImageUpload,AddressSelect,JEditor},
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 2 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        createUser:{rules: [{ required: true, message: '请输入更新人!' }]},
        updateUser:{rules: [{ required: true, message: '请输入更新人!' }]},
        },
        url: {
          add: "/mpmini/miniProblem/add",
          edit: "/mpmini/miniProblem/edit",
        },
        point:[],
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        if(this.model.longitude){
          this.point[0]=this.model.longitude;
          this.point[1]=this.model.latitude;
        }else{
          this.point=[];
        }
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'title','content','type'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },
      initAdd(val){
        this.point=val.location;
        this.model.longitude=val.location[0];
        this.model.latitude=val.location[1];
      }

    }
  }
</script>

<style lang="less" scoped>

</style>